<template>
  <div class="setting-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first" class="tab-pane">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 使用 Table 组件实现用户角色的渲染 -->
            <el-table border style="width: 100%" :data="rolesList" stripe>
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="setRoles(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRoles(scope.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRoles(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页区域 -->
            <el-pagination
              :current-page="query.page"
              :page-sizes="[10, 15, 20, 25]"
              :page-size="query.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息" class="tab-pane">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px" :model="companyInfo">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <!-- 弹框 -->
          <el-dialog
            title="提示"
            :visible.sync="showDialog"
            width="50%"
            @close="onCancel"
          >
            <el-form ref="roleDate" :model="roleDate" label-width="100px" :rules="rules">
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="roleDate.name" />
              </el-form-item>
              <el-form-item label="角色描述" prop="description">
                <el-input v-model="roleDate.description" />
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button type="primary" @click="onSubmit">确 定</el-button>
              <el-button @click="onCancel">取 消</el-button>
            </span>
          </el-dialog>
        </el-tabs>
        <!-- 分配角色权限的弹框 -->
        <el-dialog
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          title="分配权限"
          :visible.sync="showPerm"
          width="50%"
        >
          <assign-permission v-if="showPerm" :role-id="roleId" @close="showPerm = false" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getRoles, getCompanyInfo, getRoleDetail, updateRoleInfo, addRole, delRole } from '@/api/setting'
import { mapGetters } from 'vuex'
import AssignPermission from './assignPermission'
export default {
  name: 'Setting',
  components: {
    AssignPermission
  },
  data() {
    return {
      activeName: 'first',
      rolesList: [], // 角色列表
      // 查询角色列表参数
      query: {
        page: 1,
        pagesize: 10
      },
      total: 0, // 总条数
      showDialog: false, // 弹框显示
      // 收集角色资料,与后端交互
      roleDate: {
        name: '',
        description: ''
      },
      companyInfo: {}, // 公司信息
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ]

      },
      isEdit: false, // 是否为编辑状态
      roleId: '',
      showPerm: false
    }
  },
  computed: {
    ...mapGetters([
      'companyId'
    ])
  },
  created() {
    this.getRoles()
    this.getCompanyInfo()
  },
  methods: {
    // 获取公司信息
    async getCompanyInfo() {
      const res = await getCompanyInfo(this.companyId)
      // console.log(res)
      this.companyInfo = res.data
    },
    // 获取所有角色列表
    async getRoles() {
      const res = await getRoles(this.query)
      // console.log(res)
      this.rolesList = res.data.rows
      this.total = res.data.total
    },
    // 分页条数改变时触发
    handleSizeChange(newSize) {
      this.query.pagesize = newSize
      // 重新发起请求
      this.getRoles()
    },
    // 当前页改变时触发
    handleCurrentChange(newPage) {
      this.query.page = newPage
      // 重新发起请求
      this.getRoles()
    },
    setRoles(id) {
      this.showPerm = true
      this.roleId = id
    },
    // 删除角色
    async delRoles(id) {
      const confirmRes = await this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes === 'cancel') return this.$message('已取消删除')
      const res = await delRole(id)
      // 解决删除完数据分页异常
      // **判断当前条数是否为1 ,如果是,删除后条数为0,直接跳转上一页**
      if (this.rolesList.length === 1) this.query.page--
      // 不能出现负页
      if (this.query.page < 1) this.query.page = 1
      this.$message.success(res.message)
      this.getRoles()
    },
    // 编辑按钮
    async editRoles(id) {
      this.showDialog = true
      this.isEdit = true
      // 数据回显
      const res = await getRoleDetail(id)
      this.roleDate = res.data
    },
    // 新增按钮
    addRole() {
      this.showDialog = true
      this.isEdit = false
    },
    // 新增或编辑
    onSubmit() {
      this.$refs.roleDate.validate(async valid => {
        if (!valid) return
        if (this.isEdit) {
          const res = await updateRoleInfo(this.roleDate)
          // console.log(res)
          this.$message.success(res.message)
        } else {
          const res = await addRole(this.roleDate)
          // console.log(res)
          this.$message.success(res.message)
        }
        // 同一逻辑: 刷新列表  关闭弹框
        this.getRoles()
        this.showDialog = false
      })
    },
    // 取消新增或编辑
    onCancel() {
      // 这里需要实现:
      // 1.点击取消清除验证规则
      // 2.点击取消重置表单数据
      // 3.点击取消关闭弹框
      // 同时用弹框内置的关闭时的回调执行此函数
      this.$refs.roleDate.resetFields()
      this.showDialog = false
    }

  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__body {
  padding: 30px 40px;

}
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px;
}

.el-pagination {
  text-align: center;
  margin-top: 20px;
}
::v-deep .el-dialog__footer {
  text-align: center;
  .el-button+.el-button {
    margin-left: 40px;
}
}
</style>
